<?php

namespace app\api\controller;

use think\Controller;
use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;
class Sms extends Controller
{
    /**
     * 发送短信
     */
    public function send_sms($header, $data, $user)
    {
        $check=check_array($data,array('mobile','type'));
        if($check['code']!=1){
            return $check;
        }
        $mobile = $data['mobile'];
        //用户手机号
        $type = $data['type'];
        //短信类型：1：注册；2：修改密码；3：短信登录；4：微信认证；
        $check = check_format($mobile, config('regular_str.mobile'));
        if (!$check) {
            return array('code' => 2, 'msg' => '手机号格式出错', 'data' => array());
        }
        $verifi = rand(0, 9) . rand(0, 9) . rand(0, 9) . rand(0, 9) . rand(0, 9) . rand(0, 9);
        $verifi = 123456;
        //以下为发送短信
//        $send=$this->Send($mobile,$verifi,'SMS_171852688');
        $send=1;
        if(!$send){
            return array('code' => 2, 'msg' => '短信发送失败', 'data' => array());
        }
        //以上为发送短信
        //发送成功
        $end_time=time()+5*60;
        //有效时间
        $sms=array('mobile'=>$mobile,'verifi'=>$verifi,'u_type'=>$type,'u_status'=>1,'create_time'=>time(),'end_time'=>$end_time);
        $res=db('sms')->insertGetId($sms);
        if($res>0){
            return array('code' => 1, 'msg' => '短信发送成功', 'data' => array());
        }else{
            return array('code' => 2, 'msg' => '短信发送失败', 'data' => array());
        }
    }
    public static function Send($phone,$code,$moban,$more=array()){
        if(empty($more) || count($more)==0){
            $str="{\"code\":\"$code\"}";
        }else{
            $str="{\"code\":\"$code\"";
            foreach($more as $k=>$v){
                $str.=",\"".$k."\":\"".$v."\"";
            }
            $str.="}";
        }
        AlibabaCloud::accessKeyClient('LTAIEpc2v2BZrwXr', 'RQJirq6RlVEf9wyJkx23WVW977dokv')
            ->regionId('cn-hangzhou')
            ->asDefaultClient();
        try {
            $result = AlibabaCloud::rpc()
                ->product('Dysmsapi')
                // ->scheme('https') // https | http
                ->version('2017-05-25')
                ->action('SendSms')
                ->method('POST')
                ->options([
                    'query' => [
                        'RegionId' => "cn-hangzhou",
                        'PhoneNumbers' =>$phone,
                        'SignName' => "臻心严选",
                        'TemplateCode' => $moban,
                        'TemplateParam' => $str,
                    ],
                ])
                ->request();
            $send=$result->toArray();
            return $send;
            if($send['Code']=='OK'){
                return true;
            }else{
                return false;
            }
        } catch (ClientException $e) {
            return false;
        } catch (ServerException $e) {
            return false;
        }
    }
}